package cn.itcast.manus.agent.planning;


import cn.hutool.core.util.StrUtil;
import cn.itcast.manus.agent.ReActBaseAgent;
import cn.itcast.manus.constants.Constant;
import cn.itcast.manus.message.MessageSession;
import cn.itcast.manus.prompt.PromptManagement;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class ReActPlanningAgent extends ReActBaseAgent {

    @Resource
    private PromptManagement promptManagement;
    //    存放agent, key: agentName, value: 功能描述
    private final Map<String, String> agentInfo = new HashMap<>();

    public ReActPlanningAgent(MessageSession messageSession) {
        super(messageSession);
    }

    @Override
    protected String getCurrentStatus() {
        return "";
    }


    protected boolean isStatusSignificantChanged() {
//        计划Agent每次的action只需要执行一个, 后面的action不再执行
        return true;
    }


    @Override
    protected void addInitInput(List<Message> inputList, String task) {
//        添加系统消息
        inputList.add(SystemMessage.builder()
                .text(promptManagement.getPrompt(Constant.Prompts.PLANNING_SYSTEM))
                .build());

//        添加用户消息
        inputList.add(UserMessage.builder()
                .text(this.getPromptPlanningUserTask(task))
                .build());

//        添加说明
        inputList.add(UserMessage.builder()
                .text(this.getPromptPlanningUserTask(task))
                .build());
    }

    private String getPromptPlanningUserTask(String task) {
        String promptPlanningSystem = this.promptManagement.getPrompt(Constant.Prompts.PLANNING_USER_TASK);
        Map<String, String> param = Map.of(
                Constant.TASK, task,
                Constant.AGENT_DATA, String.valueOf(agentInfo)
        );
        return StrUtil.format(promptPlanningSystem, param);
    }
}
